//
//  PTBasePageViewController.m
//  PlushGame
//
//  Created by aron on 2017/11/23.
//  Copyright © 2017年 aron. All rights reserved.
//

#import "PTBasePageViewController.h"

@interface PTBasePageViewController ()

@end

@implementation PTBasePageViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self preViewDidLoadContainer];
    [self.container addSubview:self.bodyView];
    [self.container addSubview:self.headerView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)reloadData {
    [self.headerView reloadData];
    [self.bodyView reloadData];
}

- (void)preViewDidLoadContainer {
    
}

- (UIView*)container {
    UIView* container = nil;
    if ([self.dataSource respondsToSelector:@selector(pt_container)]) {
        container = [self.dataSource pt_container];
    } else {
        container = self.view;
    }
    return container;
}

#pragma mark - ......::::::: Lazy load :::::::......

- (PTBasePageHeaderView *)headerView {
    if (nil == _headerView) {
        _headerView = [[PTBasePageHeaderView alloc] init];
        _headerView.dataSource = self;
        _headerView.delegate = self;
    }
    return _headerView;
}

- (PTBasePageBodyView *)bodyView {
    if (nil == _bodyView) {
        _bodyView = [[PTBasePageBodyView alloc] init];
        _bodyView.dataSource = self;
        _bodyView.delegate = self;
    }
    return _bodyView;
}


#pragma mark - ......::::::: PTPageHeaderViewDelegate, PTPageHeaderViewDataSource :::::::......

- (void)headerView:(PTBasePageHeaderView *)headerView didSelectItemAtIndex:(NSInteger)index {
    [self.bodyView moveToIndex:index];
}

- (NSInteger)numberOfItemsInHeaderView:(PTBasePageHeaderView *)headerView {
    return self.pageHeaderTitles.count;
}

- (UIView *)pageView:(PTBasePageHeaderView *)headerView headerItemViewAtIndex:(NSInteger)index {
    return nil;
}

- (id)pageView:(PTBasePageHeaderView *)headerView headerItemAtIndex:(NSInteger)index {
    return self.pageHeaderTitles[index];
}

#pragma mark - ......::::::: PTPageBodyViewDelegate, PTPageBodyViewDataSource :::::::......

- (void)bodyView:(PTBasePageBodyView *)bodyView didSelectItemAtIndex:(NSInteger)index {
    [self.headerView moveToIndex:index];
}

- (void)bodyView:(PTBasePageBodyView *)bodyView didScrollWithOffset:(CGFloat)offset {
    [self.headerView animateMoveToOffset:offset];
}

- (NSInteger)numberOfItemsInBodyView:(PTBasePageBodyView *)bodyView {
    return self.pageBodyViewControllers.count;
}

- (UIViewController *)bodyView:(PTBasePageBodyView *)bodyView viewControllerAtIndex:(NSInteger)index {
    if (index >= 0 && index < self.pageBodyViewControllers.count) {
        return self.pageBodyViewControllers[index];
    }
    return nil;
}


@end
